﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Collections;
using System.Data;
using System.Data.SqlClient;
using System.ComponentModel;
using DoAnWeb.DTO ;

namespace DoAnWeb.DAO
{
    public class BinhLuanDAO
    {
        DataProvider dp = new DataProvider();

        public BindingList<BinhLuanDTO> GetList_TinKhuyenMai(int _Ma)
        {
            if (dp.OpenConnect())
            {
                SqlParameter[] sqlParameter = 
                { 
                    new SqlParameter("@MaTinKhuyenMai", _Ma) 
                };
                DataTable dt = dp.SqlExcuteQuery("uproc_GetList_BinhLuan_TinKhuyenMai", sqlParameter);
                dp.CloseConnect();
                BindingList<BinhLuanDTO> List = new BindingList<BinhLuanDTO>();
                foreach (DataRow row in dt.Rows)
                {
                    BinhLuanDTO DTO = new BinhLuanDTO();
                    DTO.MaBinhLuan = int.Parse(row["MaBinhLuan"].ToString());
                    DTO.MaTinKhuyenMai = int.Parse(row["MaTinKhuyenMai"].ToString());
                    DTO.TenTaiKhoan = row["TenTaiKhoan"].ToString();
                    DTO.NoiDung = row["NoiDung"].ToString();
                    DTO.HinhAnh = row["HinhAnh"].ToString();
                    if (int.Parse(row["DaXoa"].ToString()) ==0)
                        DTO.DaXoa ="Bình thường" ;
                    else
                        DTO.DaXoa = "Đã Xóa";
                    List.Add(DTO);
                }
                return List;
            }
            return null;
        }
        
        public bool Delete(int _Ma)
        {
            if (dp.OpenConnect())
            {
                SqlParameter[] sqlParameter = 
                { 
                    new SqlParameter("@MaBinhLuan", _Ma) 
                };
                bool kq = dp.SqlExcuteNonQuery("uproc_Delete_BinhLuan", sqlParameter);
                dp.CloseConnect();
                return kq;
            }
            return false;
        }

        public bool CheckDelete(int _Ma)
        {
            if (dp.OpenConnect())
            {
                SqlParameter[] sqlParameter = { new SqlParameter("@MaBinhLuan", _Ma) };
                bool kq = dp.SqlExcuteNonQuery("uproc_CheckDelete_BinhLuan", sqlParameter);
                dp.CloseConnect();
                return kq;
            }
            return false;
        }

        public bool UnCheckDelete(int _Ma)
        {
            if (dp.OpenConnect())
            {
                SqlParameter[] sqlParameter = { new SqlParameter("@MaBinhLuan", _Ma) };
                bool kq = dp.SqlExcuteNonQuery("uproc_UnCheckDelete_BinhLuan", sqlParameter);
                dp.CloseConnect();
                return kq;
            }
            return false;
        }

        public bool Add(BinhLuanDTO _DTO)
        {
            if (dp.OpenConnect())
            {
                SqlParameter[] sqlParameter = 
                { 
                    new SqlParameter("@TenTaiKhoan", _DTO.TenTaiKhoan), 
                    new SqlParameter("@MaTinKhuyenMai", _DTO.MaTinKhuyenMai), 
                    new SqlParameter("@NoiDung", _DTO.NoiDung)
                };
                bool kq = dp.SqlExcuteNonQuery("uproc_Add_BinhLuan", sqlParameter);
                dp.CloseConnect();
                return kq;
            }
            return false;

        }
        
    }
}